import player from "./Player";
const {ccclass, property} = cc._decorator;

@ccclass
export default class Skill extends cc.Component {


    @property(cc.Node)
    canvas: cc.Node = null;

    @property({type: cc.Node, displayName: "角色身上的粒子特效", tooltip: "角色身上的粒子特效"})
    particle: cc.Node = null;

    @property({type: cc.Node, displayName: "疾跑特效", tooltip: "疾跑特效"})
    SE_run_fast: cc.Node = null;

    @property({type: cc.Node, displayName: "相机", tooltip: "相机"})
    cam: cc.Node = null;

    @property({type: cc.Prefab, displayName: "动画雷电预制体", tooltip: "动画雷电预制体"})
    thunder: cc.Prefab = null;




    @property({type: player, displayName: "player脚本所在节点", tooltip: "player脚本所在节点"})
    player: player = null;






    @property({type: cc.Float, displayName: "第一个加血技能时间持续多久", tooltip: "第一个加血技能时间持续多久"})
    skill0_num: number = 3;

    @property({type: cc.Float, displayName: "第三个雷电技能时间持续多久", tooltip: "第三个雷电技能时间持续多久"})
    skill2_num: number = 5;

    @property({type: cc.Float, displayName: "免疫无敌技能持续多久", tooltip: "免疫无敌技能持续多久"})
    skill4_num: number = 3;

    @property({type: cc.Float, displayName: "保护罩技能持续多久", tooltip: "保护罩技能持续多久，不包括无敌时间"})
    skill5_num: number = 5;

    @property({type: cc.Float, displayName: "快速跑技能持续多久", tooltip: "快速跑技能持续多久"})
    skill6_num: number = 12;




    @property({type: cc.Prefab, displayName: "动画莲花预制体", tooltip: "动画莲花预制体"})
    lianhua: cc.Prefab = null;

    @property({type: cc.Prefab, displayName: "冲刺技能动画特效预制体", tooltip: "冲刺技能动画特效预制体"})
    sprint: cc.Prefab = null;

    @property({type: cc.Prefab, displayName: "光柱动画特效预制体", tooltip: "光柱动画特效预制体"})
    LC: cc.Prefab = null;

    @property({type: cc.Prefab, displayName: "击退动画特效预制体", tooltip: "击退动画特效预制体"})
    repel: cc.Prefab = null;

    @property({type: cc.Prefab, displayName: "弓箭动画特效预制体", tooltip: "弓箭动画特效预制体"})
    bow: cc.Prefab = null;



    // 是否开始第一个技能
    is_skill0: boolean = false;



    update (dt: number) {
        let self = this;

        // 如果第一个技能开始
        if (this.is_skill0 == true) {
            // 获取满血的三分之一
            let n = this.player.full_HP / 3;
            // 让角色在规定的时间内加上满血量的三分之一
            this.player.cur_HP += (n * dt) / this.skill0_num;

            // 定时器 规定时间后不再释放技能
            this.scheduleOnce(function () {
                self.is_skill0 = false;
            }, this.skill0_num);
        }


    }



    // 无敌专用函数   num为几秒恢复
    invincible (num: number) {
        let self = this;

        // 角色变透明
        this.player.player.node.opacity = 120;

        // 分组为无敌
        this.player.player.node.group = "invincible";
        // 在规定时间后不无敌
        this.scheduleOnce(function () {
            self.player.player.node.group = "player";
            self.player.player.node.opacity = 255;
        }, num);
    }




    // 荷花眷顾 持续回血
    skill0 () {
        let self = this;

        // 开始释放技能  详见update
        this.is_skill0 = true;

        // 生成一个动画莲花
        let node = cc.instantiate(this.lianhua);
        // 父节点为canvas
        node.parent = this.canvas;
        // 分组为UI 避免相机拍不到
        node.group = "skill";
        // 坐标为0 0
        node.position = cc.v3(0, 0);

        // 显示角色身上的粒子特效
        this.particle.active = true;

        // 规定时间后销毁莲花 并隐藏粒子特效
        this.scheduleOnce(function () {
            node.destroy();
            self.particle.active = false;
        }, this.skill0_num);
    }


    // 神之护盾 强化攻击力5秒
    skill1 () {
        let self = this;

        // 显示粒子特效
        this.particle.active = true;

        // 将攻击力提升至200% 并在规定时间内恢复
        this.player.ATK_promote = 2;
        this.scheduleOnce(function () {
            // 恢复攻击力并隐藏粒子特效
            self.player.ATK_promote = 1;
            self.particle.active = false;
        }, 8);
    }


    // 闪电雷击     召唤闪电造成大量伤害
    skill2 () {
        let self = this;
        
        // 创建闪电预制体
        let node = cc.instantiate(this.thunder);
        // 父节点为canvas
        node.parent = this.canvas;
        // 设置坐标
        node.position = cc.v3(0, 0);
        // this.thunder.position = this.cam.position;

        // 规定时间后销毁闪电
        this.scheduleOnce(function () {
            node.destroy();
        }, this.skill2_num);
    }


    // 冲击波 发出冲击波对敌人造成伤害
    skill3 () {
        // 创建冲击波预制体
        let node = cc.instantiate(this.sprint);
        // 父节点为canvas
        node.parent = this.canvas;
        // 设置坐标
        node.position = cc.v3(this.player.player.node.x, this.player.player.node.y + 50);

        // 如果角色朝右
        if (this.player.player.node.scaleX > 0) {
            // 移动动作
            let m = cc.moveBy(0.51, cc.v2(500, 0));
            // 回调函数
            let f = cc.callFunc(function () {
                // 销毁
                node.destroy();
            });

            // 两个动作结合在一起
            let ac = cc.sequence(m, f);
            // 执行动作
            node.runAction(ac);
        }
        // 如果角色朝左 
        else {
            // 移动动作
            let m = cc.moveBy(0.51, cc.v2(-500, 0));
            // 回调函数
            let f = cc.callFunc(function () {
                // 销毁
                node.destroy();
            });

            // 两个动作结合在一起
            let ac = cc.sequence(m, f);

            // 执行动作
            node.runAction(ac);
        }

        // 20秒后销毁冲击波
        this.scheduleOnce(function () {
            node.destroy();
        }, 20);
    }


    // 免疫 无视所有伤害三秒
    skill4 () {
        let self = this;

        // 显示粒子特效
        this.particle.active = true;

        // 无敌   规定时间后恢复
        this.invincible(this.skill4_num);

        // 规定时间后隐藏粒子特效
        this.scheduleOnce(function () {
            self.particle.active = false;
        }, this.skill4_num);
    }


    // 保护罩   调高攻击力和防御力至150%   提高暴击率至120%   5秒恢复
    skill5 () {
        let self = this;


        // 显示粒子特效
        this.particle.active = true;


        // 获取暴击率
        let CRI = this.player.CRI_num;
        // 提高暴击率
        this.player.CRI_num = this.player.CRI_num * 1.2;

        // 提高攻击力和防御力
        this.player.ATK_promote = this.player.ATK_promote * 1.5;
        this.player.AC_promote = this.player.AC_promote * 1.5;

        // 无敌  1秒后恢复
        this.invincible(1);


        // 规定时间后全部恢复
        this.scheduleOnce(function () {
            self.particle.active = false;
            self.player.ATK_promote = 1;
            self.player.AC_promote = 1;
            self.player.CRI_num = CRI;
        }, this.skill5_num);
    }


    // 飞速跑   提高速度至180%   持续12秒
    skill6 () {
        let self = this;

        // 获取角色行走速度
        let s = this.player.walk_speed;
        // 提高行走速度
        this.player.walk_speed = this.player.walk_speed * 1.8;

        // 显示疾跑特效
        this.SE_run_fast.active = true;

        // 规定时间后全部恢复
        this.scheduleOnce(function () {
            self.player.walk_speed = s;
            self.SE_run_fast.active = false;
        }, this.skill6_num);
    }


    // 击退   击退敌人
    skill7 () {
        // 创建击退技能预制体
        let node = cc.instantiate(this.repel);
        // 父节点为canvas
        node.parent = this.canvas;
        // 设置坐标
        node.position = cc.v3(this.player.player.node.x, this.player.player.node.y + 50);

        // 如果角色朝右边
        if (this.player.player.node.scaleX > 0) {
            // 移动动作
            let m = cc.moveBy(0.7, cc.v2(500, 0));
            // 回调函数
            let f = cc.callFunc(function () {
                // 销毁
                node.destroy();
            });

            // 两个动作结合在一起
            let ac = cc.sequence(m, f);

            // 执行动作
            node.runAction(ac);

            // 缩放
            node.scaleX = -1;
        } else {
            // 移动动作
            let m = cc.moveBy(0.51, cc.v2(-500, 0));
            // 回调函数
            let f = cc.callFunc(function () {
                // 销毁
                node.destroy();
            });

            // 两个动作结合在一起
            let ac = cc.sequence(m, f);

            // 执行动作
            node.runAction(ac);

            // 缩放
            node.scaleX = 1;
        }

        // 20秒后销毁
        this.scheduleOnce(function () {
            node.destroy();
        }, 20);
    }


    // 弓箭   召唤弓箭阵
    skill8 () {
        // 创建弓箭阵预制体
        let node = cc.instantiate(this.bow);
        // 父节点为canvas
        node.parent = this.canvas;

        // 根据人物朝向来设置坐标
        if (this.player.player.node.scaleX > 0) {
            node.position = cc.v3(this.player.player.node.x + 230, this.player.player.node.y + 120);
        } else {
            node.position = cc.v3(this.player.player.node.x - 230, this.player.player.node.y + 120);
        }

        // 20秒后销毁
        this.scheduleOnce(function () {
            node.destroy();
        }, 3.1);
    }



}
